#! /usr/bin/env python

#
# 
# Copyright (c) 2007-2013, University of California / Singapore Management University
#   Lingxiao Jiang         <lxjiang@ucdavis.edu> <lxjiang@smu.edu.sg>
#   Ghassan Misherghi      <ghassanm@ucdavis.edu>
#   Zhendong Su            <su@ucdavis.edu>
#   Stephane Glondu        <steph@glondu.net>
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the University of California nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# 
#

# Usage: $0 <post-precossed cluster file> <lang>
# output to stdout

import re
import sys
import os
#import shutil

if len(sys.argv) != 3:
    print >> sys.stderr, "Usage: ", sys.argv[0], " <clone report> <lang>"
    sys.exit(1)
elif sys.argv[2] != 'c' and sys.argv[2] != 'java' and sys.argv[2] != 'php':
    print >> sys.stderr, "We only support c, java, and php for now"
    sys.exit(1)

# check executable paths
filterpath = os.environ.get("DECKARD_DIR")
if filterpath==None:
    filterpath = '.'
filterpath = filterpath + '/src/main'
if not os.path.exists(filterpath):
    print >> sys.stderr, "cannot find bugfilters in", filterpath
    sys.exit(1)

def readclusters( file ):
    f= open(file,'r')
    clusters= []
    cluster= []
    for line in f.readlines():
        if line == '\n' or line == '':
            if len(cluster) > 0:
                clusters.append(cluster)
                cluster=[]
            continue
        cluster.append(line)
    f.close()
    if len(cluster) > 0:
        clusters.append(cluster)
    return clusters

clusters= readclusters( sys.argv[1] )
#os.system('echo -n "" > ' + sys.argv[1] + '.bug')
for cluster in clusters:
    f=open( sys.argv[1] + '.clustertmp', 'w' )
    for line in cluster:
        f.write(line)
    f.close()
    #raw_input("Press Enter to continue...")
# meaning of IDs to cbugfilters:
# 0: apply all filters and filter it iff all filters agree.
# 1: apply context comparison
# 2: apply context comparison and condition comparison
# 3: apply variable filtering
# none: apply all filters and filter it iff any filter says yes
    if sys.argv[2] == 'c':
        os.system(filterpath + '/cbugfilters 2 < ' + sys.argv[1] + '.clustertmp')
    elif sys.argv[2] == 'java':
        os.system(filterpath + '/jbugfilters 2 < ' + sys.argv[1] + '.clustertmp')
    elif sys.argv[2] == 'php':
        os.system(filterpath + '/phpbugfilters 2 < ' + sys.argv[1] + '.clustertmp')
    else:
        print >> sys.stderr, "parameter error for bugfiltering."
        sys.exit(1)
#    os.system('cbugfilters < ' + sys.argv[1] + '.clustertmp >> ' + sys.argv[1] + '.bug')
#    os.system('echo "" >> ' + sys.argv[1] + '.bug')
os.remove(sys.argv[1] + '.clustertmp')

